package jdbc;

import dao.AccountDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import pojo.Account;


public class JdbcTemplateTest {
    @Test
    public void mainTest(){
        findAllAccountTest();
        findAccountByIdTest();
        updateAccountTest();
        deleteAccountTest();
        addAccountTest();
    }
    @Test
    public void findAllAccountTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        AccountDao accountDao = (AccountDao) context.getBean("accountDao");
        System.out.println(accountDao.findAllAccount());
    }
    @Test
    public void findAccountByIdTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        AccountDao accountDao = (AccountDao) context.getBean("accountDao");
        System.out.println(accountDao.findAccount(1));
    }
    @Test
    public void addAccountTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        AccountDao accountDao = (AccountDao) context.getBean("accountDao");
        Account account = new Account();
        account.setUsername("jfldjlfajlfja");
        account.setBalance(123.3);
        System.out.println(accountDao.addAccount(account));
    }
    @Test
    public void deleteAccountTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        AccountDao accountDao = (AccountDao) context.getBean("accountDao");
        System.out.println(accountDao.deleteAccount(3));
    }
    @Test
    public void updateAccountTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        AccountDao accountDao = (AccountDao) context.getBean("accountDao");
        Account account = new Account();
        account.setId(1);
        account.setUsername("jdljl");
        account.setBalance(12111.22);
        System.out.println(accountDao.updateAccount(account));
    }
}
